package com.example.util;

/**
 * 描述:
 阿里云短信API依赖Jar包
 <dependency>
 <groupId>com.aliyun</groupId>
 <artifactId>aliyun-java-sdk-core</artifactId>
 <version>4.4.6</version>
 </dependency>
 <dependency>
 <groupId>com.aliyun</groupId>
 <artifactId>aliyun-java-sdk-dysmsapi</artifactId>
 <version>1.1.0</version>
 </dependency>
 */
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class SmsService {
    private static final Logger logger = LoggerFactory.getLogger(SmsService.class);


    /**
     *@Title: aliyunSendSms
     *@Description: TODO(阿里云发送短信方法)
     *@param accessKeyId        你的accessKeyId
     *@param accessKeySecret    你的accessKeySecret
     *@param phoneNumbers        待发送手机号。支持以逗号分隔的形式进行批量调用，批量上限为1000个手机号码
     *@param signName            短信签名-可在短信控制台中找到
     *@param templateCode        短信模板-可在短信控制台中找到
     *@param templateParam        "{\"name\":\"Tom\", \"code\":\"123\"}"
     */
    public static String aliyunSendSms(String accessKeyId,String accessKeySecret,String phoneNumbers,String signName,String templateCode,String templateParam) {
        try {
            // 设置超时时间-可自行调整
            System.setProperty("sun.net.client.defaultConnectTimeout", "10000");
            System.setProperty("sun.net.client.defaultReadTimeout", "10000");
            // 初始化ascClient需要的几个参数
            final String product = "Dysmsapi";// 短信API产品名称（短信产品名固定，无需修改）
            final String domain = "dysmsapi.aliyuncs.com";// 短信API产品域名（接口地址固定，无需修改）
            IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyId,accessKeySecret);
            DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou", product, domain);
            IAcsClient acsClient = new DefaultAcsClient(profile);
            SendSmsRequest request = new SendSmsRequest();
            request.setMethod(MethodType.POST);
            request.setPhoneNumbers(phoneNumbers);
            request.setSignName(signName);
            request.setTemplateCode(templateCode);
            request.setTemplateParam(templateParam);
            // 请求失败这里会抛ClientException异常
            SendSmsResponse sendSmsResponse = acsClient.getAcsResponse(request);
            if (StringStrUtils.isNotBlank(sendSmsResponse.getCode()) && "OK".equals(sendSmsResponse.getCode())) {
                return "发送成功";
            }else {
                logger.error("发送失败:"+sendSmsResponse.getMessage());
                return "发送失败:"+sendSmsResponse.getMessage();
            }
        } catch (Exception e) {
            e.printStackTrace();
            return "发送异常！"+e.getMessage();
        }
    }

    /**
     *@Title: aliyunSendSms_ltzy
     *@Description: (阿里云发送短信-中原优采)
     *@param phoneNumbers    待发送手机号。支持以逗号分隔的形式进行批量调用，批量上限为1000个手机号码
     *@param templateCode    短信模板-可在短信控制台中找到
     *@param templateParam    "{\"name\":\"Tom\", \"code\":\"123\"}"
     */
    public static String aliyunSendSms_zyyc(String phoneNumbers,String templateCode,String templateParam) {
        final String signName = "中原优采";
        final String accessKeyId = "LTAI4GJq6hew64Md5aRkCMLT";
        final String accessKeySecret = "KtIRSFJKHvmh2G43L5eKn7bMA3F6UQ";
        return aliyunSendSms(accessKeyId, accessKeySecret, phoneNumbers, signName, templateCode, templateParam);
    }


}
